// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#define WM_TASKBARNOTIFIERCLICKED	WM_USER + 123
#define WM_TRAYNOTIFY				WM_USER + 124
#define WM_ZONENAME					WM_USER + 125
#define WM_ON_CONNECT				WM_USER + 126
#define WM_ON_DISCONNECT			WM_USER + 127
#define WM_ON_READ					WM_USER + 128
#define WM_ON_STATUS				WM_USER + 129
#define WM_ON_LOGIN					WM_USER + 130
#define WM_ON_GET_ROOMS_PROPS		WM_USER + 131
#define WM_ON_ADV_FILE				WM_USER + 132

/////////////////////////////////////////////////////////////////////////////

struct login_info
{
	boost::guid							identiy_id_;
	std::string							name_;
	std::string							pwd_;
	std::string							email_;
};

/////////////////////////////////////////////////////////////////////////////

class CMainDlg : public CDialogImpl<CMainDlg>, 
	public CDialogResize<CMainDlg>,
		public CUpdateUI<CMainDlg>,
			public CMessageFilter, 
				public CIdleHandler,
					public cm::client_server::basic_subscriber
{
public:
	CMainDlg();
	virtual ~CMainDlg();
private:
	void ShowText(LPCTSTR Text);
private:
	void read_config_parameters();
	void write_config_parameters();
private:// Workarround
	void SetPaneWidths(int* arrWidths, int nPanes);
private:
	void Login();
private:
	void AddZone(LPCTSTR ZoneName);
	void ResetComboZone();
public:
	enum { IDD = IDD_MAINDLG };

	virtual BOOL PreTranslateMessage(MSG* pMsg);
	virtual BOOL OnIdle();

	BEGIN_UPDATE_UI_MAP(CMainDlg)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CMainDlg)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_SIZE, OnSize)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		
		MESSAGE_HANDLER(WM_TASKBARNOTIFIERCLICKED, OnTaskbarNotifierClicked)
		MESSAGE_HANDLER(WM_TRAYNOTIFY, OnTrayNotify)
		
		MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
		MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
		MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)

		MESSAGE_HANDLER(WM_DRAWITEM, OnDrawItem)

		COMMAND_HANDLER(IDC_BUTTON_HIDE, BN_CLICKED, OnBnClickedButtonHide)

		COMMAND_ID_HANDLER(ID_TAKE_A_NAME, OnTakeAName)
		
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		COMMAND_ID_HANDLER(IDOK, OnOK)
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)

		COMMAND_HANDLER(IDC_COMBO_ZONE, CBN_SELCHANGE, OnCbnSelchangeComboZone)
		COMMAND_HANDLER(IDC_COMBO_ZONE, CBN_DROPDOWN, OnCbnDropdownComboZone)
		MESSAGE_HANDLER(WM_ZONENAME, OnZoneName)
		COMMAND_HANDLER(IDC_BUTTON_ADD_ROOM, BN_CLICKED, OnBnClickedButtonAddRoom)

		MESSAGE_HANDLER(WM_ON_CONNECT, OnConnect)
		MESSAGE_HANDLER(WM_ON_DISCONNECT, OnDisconnect)
		MESSAGE_HANDLER(WM_ON_STATUS, OnStatus)
		MESSAGE_HANDLER(WM_ON_LOGIN, OnLogin)
		MESSAGE_HANDLER(WM_ON_READ, OnRead)
		MESSAGE_HANDLER(WM_ON_GET_ROOMS_PROPS, OnGetRoomsProps);
		MESSAGE_HANDLER(WM_ON_ADV_FILE, OnGetAdvFile);

		NOTIFY_HANDLER(IDC_STATUS_BAR,NM_CLICK,OnStatusBarClick);
		NOTIFY_CODE_HANDLER(TTN_NEEDTEXT, OnToolTipNotify)

		CHAIN_MSG_MAP(CDialogResize<CMainDlg>)

		REFLECT_NOTIFICATIONS()
	END_MSG_MAP()

	BEGIN_DLGRESIZE_MAP(CMainDlg)
		DLGRESIZE_CONTROL(IDC_COMBO_ZONE,DLSZ_SIZE_X)
		DLGRESIZE_CONTROL(IDC_BUTTON_ADD_ROOM,DLSZ_MOVE_X)
		DLGRESIZE_CONTROL(IDC_SPLIT,DLSZ_SIZE_X | DLSZ_SIZE_Y)
		DLGRESIZE_CONTROL(IDC_BUTTON_HIDE,DLSZ_MOVE_X)
		DLGRESIZE_CONTROL(IDC_BUTTON_PREFERENCES,DLSZ_MOVE_X)
		DLGRESIZE_CONTROL(IDC_STATUS_BAR,DLSZ_SIZE_X | DLSZ_MOVE_Y)
		DLGRESIZE_CONTROL(IDC_STATIC_THUMBNAIL1,DLSZ_MOVE_X)
		DLGRESIZE_CONTROL(IDC_STATIC_THUMBNAIL2,DLSZ_MOVE_X)
		DLGRESIZE_CONTROL(IDC_STATIC_THUMBNAIL3,DLSZ_MOVE_X)
	END_DLGRESIZE_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

	LRESULT OnTaskbarNotifierClicked(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	
	LRESULT OnTrayNotify(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

	BOOL OnToolTipNotify(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/);
	BOOL OnStatusBarClick(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/);

	LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnLButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnLButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

	LRESULT OnBnClickedButtonHide(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

	LRESULT OnTakeAName(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

	LRESULT OnDrawItem(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

	LRESULT OnCbnSelchangeComboZone(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnCbnDropdownComboZone(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnZoneName(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnBnClickedButtonAddRoom(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

	LRESULT OnConnect(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnDisconnect(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnLogin(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnStatus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnRead(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnGetRoomsProps(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnGetAdvFile(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

	void CloseDialog(int nVal);
private:// Callbacks from server
	virtual void on_connected(const cm::client_server::session_id& s_id);
	virtual void on_disconnected();
	virtual void on_login(const boost::guid& idy_id,const std::string& name,const std::string& email);
	virtual void on_status(const cm::client_server::size_t& in_room_users,const cm::client_server::size_t& total_users);
	virtual void on_read(const cm::client_server::message& msg);
	virtual void on_get_rooms_props(const cm::client_server::rooms_props& props);
	virtual void on_adv_file(cm::client_server::raw_file_ptr ptr_adv_file);
private:
	boost::asio::io_service		io_service_;
	cm::client_server::client	client_;
	boost::thread*				ptr_client_thread_;// E' un puntatore in quanto il thread deve partire dopo la connessione del client
private:
	bool									connected_;
	cm::client_server::session_id			session_id_;
	login_info								login_info_;
	boost::guid								room_id_;
	bool									remember_login_;
	bool									remember_pwd_;
	cm::client_server::server_status_info	server_status_info_;
private:
	cm::Windows::CTaskbarNotifier		m_TaskbarNotifier;
	cm::Windows::CSoftNotifier			m_SoftNotifier;
	cm::Windows::CTrayNotifyIcon		m_TrayNotifyIcon;
	cm::Windows::CButtonMenu			m_ButtonPreferences;
	cm::Windows::CAvatarCtrl			m_AvatarPicture[3];
	cm::Windows::CComboBoxAutoComplete	m_ComboBoxZone;
	CButtonST							m_AddZoneButton;
	CToolTipCtrl						m_ToolTipCtrl;
	CHorSplitterWindow					m_HorSplitterWindow;
	CMultiPaneStatusBarCtrl				m_StatusBar;
private:
	LONG StartXPos;
	LONG StartYPos;
	bool InMove;
public:
	LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
};
